package stardust.product.spider.http;


import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import stardust.product.spider.URLSource;


public class SimpleURLSource implements URLSource
{
	
	protected LinkedBlockingQueue<URLEntity> urlList = new LinkedBlockingQueue<URLEntity>(100);
	
	private SimpleURLSourceFetcher fetcher;
	
	public SimpleURLSource(SimpleURLSourceFetcher fetcher)
	{
		this.fetcher = fetcher;
	}
	
	public boolean hasMore()
	{
		return !this.urlList.isEmpty();
	}

	public synchronized URLEntity next() throws InterruptedException
	{
		if (!this.hasMore())
		{
			this.fetcher.fetch(this);
		}
		
		return this.urlList.poll(1, TimeUnit.NANOSECONDS);
	}
	
	public boolean append(URLEntity urlEntity)
	{
		try
		{
			return this.urlList.add(urlEntity);
		}
		catch (IllegalStateException e)
		{
			return false;
		}
	}
	
	public static interface SimpleURLSourceFetcher
	{
		
		void fetch(SimpleURLSource urlSource);
		
	}

}
